package com.vpen.client.test;

import com.vpen.client.demo.DemoWebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

/**
 * 描述: 使用sockJs测试
 *
 * @author 韦鹏
 * @date 2022/11/28 12:57
 */
@Slf4j
public class SockJsClientApp {
    public static final String URL = "ws://localhost:8000/sockJs";

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {

        /**
         *
         * 支持sockJs客户端
         * */
        List<Transport> transports = new ArrayList<>(2);
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());
        SockJsClient sockJsClient = new SockJsClient(transports);


        WebSocketStompClient socketStompClient = new WebSocketStompClient(sockJsClient);

        /**
         * 使用多种消息转换器
         * */
        List<MessageConverter> converters = new ArrayList<>();
        converters.add(new MappingJackson2MessageConverter());
        converters.add(new StringMessageConverter());

        // 设置消息转换器：否则消息无法接收到
        socketStompClient.setMessageConverter(new CompositeMessageConverter(converters));

        // 握手用到的headers
        WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders();
        handshakeHeaders.add("ipAddr", "192.168.0.1");

        // stomp连接用到的headers，可以放token
        StompHeaders connectHeaders = new StompHeaders();
        connectHeaders.add("name", "vpen");


        // 连接websocket服务器
        ListenableFuture<StompSession> future = socketStompClient.connect(URL, handshakeHeaders, connectHeaders, new MyStompSessionHandler());

        // 回调方法
        future.addCallback(new MyListenableFutureCallback());
        StompSession stompSession = future.get();


        stompSession.subscribe("/topic/hello", new MyStompSessionHandler());


        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            stompSession.send("/app/hello1", str);
        }

    }
}
